//
//  ImageViewController.m
//  nonghubao
//
//  Created by 王 文彬 on 13-3-16.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "ImageViewController.h"
#import "UIImageView+WebCache.h"
#import "Helper.h"

@interface ImageViewController ()

@end

@implementation ImageViewController

@synthesize delegate = _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithURL:(NSURL *)sourceURL
{
    self = [super init];
    if (self) {
        _sourceURL = sourceURL;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton* closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, 30)];
    closeButton.titleLabel.font = _SYS_FONT_003;
    [closeButton setTitle:_L(@"btn_close_01") forState:UIControlStateNormal];
    [closeButton setBackgroundImage:[UIImage imageNamed:@"more_btn_001_n"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(closeButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:closeButton];
    
    
    UIButton* saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, 30)];
    saveButton.titleLabel.font = _SYS_FONT_003;
    [saveButton setTitle:_L(@"btn_save_01") forState:UIControlStateNormal];
    [saveButton setBackgroundImage:[UIImage imageNamed:@"more_btn_001_n"] forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(saveButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveButton];
    
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    self.view.backgroundColor =[UIColor blackColor];

    _scrollView = [[UIScrollView alloc] init];
    _scrollView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_scrollView];
    
    
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _activityIndicator.frame = CGRectMake(0, 0, 50, 50);
    _activityIndicator.center = self.view.center;
    [_activityIndicator startAnimating];
    [self.view addSubview:_activityIndicator];   //注意，是加在self.view而不是_scrollView的
    
    _imageView = [[UIImageView alloc] init];
    [_scrollView addSubview:_imageView];
    _imageView.backgroundColor = [UIColor yellowColor];


    NSLog(@"source %@", _sourceURL);
    [self performSelector:@selector(loadImage) withObject:nil afterDelay:0.3];
    
//    __block UIImageView* aImageView = imageView;
//    [_imageView setImageWithURL:_sourceURL placeholderImage:nil options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
//
//        if (error == nil) {
//
//            _image = image;
//            
//            CGSize size = [Helper sizeOfImage:image forStyle:PictureStyleFullFrame];
//            float x = (self.view.frame.size.width - size.width) / 2;
//            float y = (self.view.frame.size.height - size.height) / 2;
//            y = y < 0 ? 0 : y;
//
//            aImageView.frame = CGRectMake(0, 0, size.width, size.height);
//            scrollView.contentSize = CGSizeMake(size.width, size.height);
//            scrollView.frame = CGRectMake(x, y, size.width, size.height);
//        }
//    }];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [_delegate imageViewControllDidDisappear];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 按钮事件
- (void)closeButtonEvent
{
    [self dismissModalViewControllerAnimated:true];
}


- (void)saveButtonEvent
{
    UIImageWriteToSavedPhotosAlbum(_image, nil, nil,nil);
    
    MBProgressHUD* hud = [[MBProgressHUD alloc] init];
    [hud setUserInteractionEnabled:false];
    [hud setMode:MBProgressHUDModeText];
    [hud performSelector:@selector(setHidden:) withObject:self afterDelay:1];
    [hud setLabelText:_L(@"title_saveimage2sys_01")];
    [self.view addSubview:hud];
    [hud show:true];
}

#pragma mark - 私有方法

- (void)loadImage
{
    _image = [UIImage imageWithData:[NSData dataWithContentsOfURL:_sourceURL]];
    _imageView.image = _image;
    
    CGSize size = [Helper sizeOfImage:_image forStyle:PictureStyleFullScreen];
    float x = (self.view.frame.size.width - size.width) / 2;
    float y = (self.view.frame.size.height - size.height) / 2;
    y = y < 0 ? 0 : y;
    
    _imageView.frame = CGRectMake(0, 0, size.width, size.height);
    _scrollView.contentSize = CGSizeMake(size.width, size.height);
    _scrollView.frame = CGRectMake(x/2, y/2, size.width * 2 > 320 ? 320 : size.width * 2, size.height * 2 > 460 ? 460 : size.height * 2);
    _scrollView.contentInset = UIEdgeInsetsMake(y / 2, x, y, x);
    
//    NSLog(@"%f %f %f %f", x, y, size.width, size.height);
//    NSLog(@"%f %f %f %f", x / 2, y / 2, size.width * 2 > 320 ? 320 : size.width * 2, size.height * 2 > 460 ? 460 : size.height * 2);
//    
    _scrollView.minimumZoomScale = 1.0;   //最小倍数
    _scrollView.maximumZoomScale = 2.0;  //最大倍数
    _scrollView.zoomScale = 1.0;  //初始时的大小。不然初始拖动会显示底色的
    _scrollView.delegate = (id)self;
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    
    [_activityIndicator stopAnimating];
    [_activityIndicator removeFromSuperview];
}


#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView; //返回ScrollView上添加的需要缩放的视图
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    //缩放操作中被调用
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    //缩放结束后被调用
}

@end
